package hu.elte.tudaskezelo.dbpedialike.ws;

import hu.elte.tudaskezelo.dbpedialike.domain.CityFind;

import java.util.ArrayList;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.ResultReceiver;
import android.util.Log;

public class QueryService extends IntentService {

	private static final int STATUS_RUNNING = 0;
	private static final int STATUS_FINISHED = 1;
	private static final int STATUS_ERROR = 2;

	final String tag = "QueryService";

	SPARQLQuery sparqlQuery = new SPARQLQuery();

	// @Inject
	// private NearestCity nearestCity;

	public QueryService() {
		super("QueryService");
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		final ResultReceiver receiver = intent.getParcelableExtra("receiver");
		String command = intent.getStringExtra("command");
		String queryParam = intent.getStringExtra("queryParam");
		Bundle b = new Bundle();
		if (command.equals("findCityQuery")) {
			receiver.send(STATUS_RUNNING, Bundle.EMPTY);

			try {

				ArrayList<Parcelable> results = sparqlQuery
						.findCityQuery(queryParam);

				// TESZTELÉSHEZ BEGIN
				results = new ArrayList<Parcelable>();
				queryParam = "Budapest";
				sparqlQuery.cityQuery(queryParam);
				Parcelable teszt = new CityFind(47.615653, 19.672981,
						"Budapest", "Magyarország");

				for (int i = 0; i < 5; i++) {
					results.add(teszt);
				}
				Log.d(tag, "result list:" + results.size());

				// TESZTELÉSHEZ END
				b.putParcelableArrayList("queryResults", results);
				receiver.send(STATUS_FINISHED, b);

			} catch (Exception e) {
				b.putString(Intent.EXTRA_TEXT, e.toString());
				receiver.send(STATUS_ERROR, b);
			}
		}
		this.stopSelf();
	}
}
